//
//  MainViewController.swift
//  demo01新浪微博
//
//  Created by 杨兵 on 15/5/10.
//  Copyright (c) 2015年 bing. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    @IBOutlet weak var mainTabBar: MainTabBar!
   
    var homevc:UINavigationController?
    
    var timer:NSTimer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 添加模块
        homevc = addChildViewControllers("Home", "首页", "tabbar_home")
        selectedVC = homevc
        
        addChildViewControllers("Profile", "我", "tabbar_profile")
        addChildViewControllers("Discover", "发现", "tabbar_discover")
        addChildViewControllers("Message", "消息", "tabbar_message_center")
        
        // 添加按钮的点击事件
        mainTabBar.composeButton .addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.delegate = self
    }

    
    func composeButtonClick() {
        let sb = UIStoryboard(name: "Compose", bundle: nil)
        let vc = sb.instantiateInitialViewController() as! UINavigationController
        presentViewController(vc, animated: true, completion: nil)
        
    }
    
    
    // 添加控制器视图
    func addChildViewControllers(sbName: String,_ title: String,_ img: String) -> UINavigationController{
        // 橙色
        self.tabBar.tintColor = UIColor.orangeColor()
        
        // 通过storyboard创建对象
        let sb = UIStoryboard(name: sbName, bundle: nil)
        let nav = sb.instantiateInitialViewController() as! UINavigationController
        
        // 设置属性,标题,图片,文字
        // nav.title = title
        nav.topViewController.title = title
        nav.tabBarItem.image = UIImage(named: img)
        nav.tabBarItem.selectedImage = UIImage(named: img + "_highlighted" )
        
        // 设置frame
        // 添加到到控制器中
        addChildViewController(nav)
        
        return nav
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        unreadmsg()
    }
    
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        timer?.invalidate()
    }
    
    
    // 添加未读微博通知
    private func unreadmsg() {
        
        // 用户登陆才能添加定时器,加载未读信息
        if userInfoShared != nil {
            timer = NSTimer(timeInterval: 10.0, target: self, selector: "loadunread", userInfo: nil, repeats: true)
            NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
        }
    }
    
    
    func loadunread() {
        Status.unreadMsg { (count) -> () in
            UIApplication.sharedApplication().applicationIconBadgeNumber = count
            if count > 0 {
                self.homevc?.tabBarItem.badgeValue = "\(count)"
                return
            }
            self.homevc?.tabBarItem.badgeValue = nil
        }
    }
    
    
    override func addChildViewController(childController: UIViewController) {
        super.addChildViewController(childController)
    }

    // 记录上一控制器
    var selectedVC: UIViewController?
}


extension MainViewController: UITabBarControllerDelegate {
    
    // 选中 代理控制器,判断上一控制器是否是homevc,是的话,就刷新
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        
        if viewController != homevc || selectedVC != homevc{
            selectedVC = viewController
            return
        }
        
        let vc = homevc?.topViewController as! HomeTableViewController
        // 选中控制器,与上一控制器都是homevc才刷新数据
        if homevc?.tabBarItem.badgeValue?.toInt() > 0 {
            vc.loadData()
            homevc?.tabBarItem.badgeValue = nil
            UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        }
        vc.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: true)
        
    }
}
